<template>
  <div class="app-container">
    <h5 class="titleInfo">菜单编辑</h5>
    <el-form class="form" ref="form" :rules="rules" :model="form" label-width="130px">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input v-model="form.menuName"></el-input>
      </el-form-item>
      <el-form-item label="所属系统">
        <Fromplatfrom @childfun="childfun" ref="subSysMsg" values="disable"></Fromplatfrom>
      </el-form-item>
      <el-form-item label>
        <p style="margin: 0;color: #a00303;">请先选择所属系统再选择上级菜单</p>
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentId">
        <el-select v-model="form.parentId" filterable placeholder="请选择">
          <el-option
            v-for="item in parentOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option
            v-for="item in statusOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单描述" prop="description">
        <el-input v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort"></el-input>
      </el-form-item>
      <el-form-item label="图标">
        <icons v-if="iconFlag" style="width:236px" :defaultIcon="form.icon" @IconChoice="getIcon"></icons>
      </el-form-item>
      <el-form-item label="url" prop="url">
        <el-input v-model="form.url"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitFn('form')">立即提交</el-button>
      </el-form-item>
    </el-form>
    <!-- <ul>
      <li>
        <el-radio label="1" v-model="parent1">备选项</el-radio>
        <ol>
          <el-radio label="1" v-model="valueArr[0]">备选项</el-radio>
          <el-radio label="2" v-model="valueArr[0]">备选项</el-radio>
          <ol>
            <el-radio label="2" v-model="valueArr[1]">备选项</el-radio>
          </ol>
        </ol>
      </li>
    </ul>-->
  </div>
</template>
<script>
import icons from "../../icons/index";
import { listAdminMenus, updateMenu, getMenuById } from "@/api/menuManage";
import Fromplatfrom from "../components/platform";
export default {
  name: "menuManageEdit",

  data() {
    return {
      valueArr: ["", ""],
      iconFlag: false,
      fileNum: 1,
      parentOption: [
        {
          value: 0,
          label: "顶级"
        }
      ],
      statusOption: [
        {
          value: 0,
          label: "开启"
        },
        {
          value: 1,
          label: "关闭"
        }
      ],
      form: {
        menuName: null,
        parentId: null,
        status: null,
        description: null,
        // code:null,
        sort: null,
        icon: null,
        url: null
      },
      rules: {
        menuName: [
          { required: true, message: "请输入菜单名称", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "请选择上级菜单", trigger: "blur" }
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        code: [{ required: true, message: "请输入权限字段", trigger: "blur" }],
        url: [{ required: true, message: "请输入url", trigger: "blur" }]
      },
      userId: null
    };
  },
  mounted() {
    this.userId = Number(this.$route.query.userId);
    this.getInfo();
    // this.getMenu();
  },
  inject: ["reload"],
  components: { icons, Fromplatfrom },
  methods: {
    childfun(val) {
      console.log(val);
      this.form.subSys = val === "" ? -1 : val;
      this.getMenu();
    },
    //获取详情
    getInfo() {
      let datas = {
        id: this.userId
      };
      getMenuById(datas)
        .then(res => {
          let resData = res.data.data;
          if (!res.data.code) {
            this.form.menuName = resData.menuName;
            this.form.parentId = resData.parentId;
            this.form.status = resData.status;
            this.form.description = resData.description;
            this.$refs.subSysMsg.getMessage(res.data.data.subSys);
            let codeArr = "";
            resData.permissionDTOS.forEach(e => {
              if (e.code) {
                codeArr += e.code;
              }
            });
            if (codeArr) {
              this.form.code = codeArr;
            }
            this.form.sort = resData.sort;
            this.form.icon = resData.icon;
            this.form.url = resData.url;
            this.iconFlag = true;
            // console.log(this.form)
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    //获取菜单下拉
    getMenu() {
      let datas = {
        pageNum: 1,
        subSys: this.form.subSys,
        pageSize: 10000
      };
      listAdminMenus(datas)
        .then(res => {
          if (!res.data.code) {
            // console.log(res.data.data.menuDTOS)
            let resData = res.data.data.menuDTOS;
            resData.forEach(e => {
              this.parentOption.push({
                value: Number(e.id),
                label: String(e.menuName)
              });
            });
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    getIcon(data) {
      this.form.icon = data.value;
    },
    // 立即提交
    submitFn(e) {
      this.$refs[e].validate(valid => {
        if (valid) {
          if (this.form.icon == "无图标") {
            this.form.icon = null;
          }
          this.form.id = this.userId;
          let datas = this.form;
          updateMenu(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.$message({
                  showClose: true,
                  message: "修改成功",
                  type: "success"
                });
                this.$router.push("/menuManageIndex");
                this.reload();
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        } else {
          this.$message({
            message: "必填项不能为空",
            type: "warning"
          });
        }
      });
    }
  }
};
</script>
<style scoped>
.app-container {
  padding: 0.1px !important;
}
</style>
